#include "lcd_init.h"
#include "stdio.h"
#include <string.h>
// #include "app.h"

typedef void (*FunType)(void);
//**********************************write function begin********************************************
void wr_cmd_4spi_8bcs(uint8_t par)
{
    LCD_DCXLow();
    LCD_CSLow();

    LCD_SCLLow();
    if (par & 0x80)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x40)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x20)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x10)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x08)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x04)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x02)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x01)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_CSHigh();
    // LCD_DCXHigh();
    // HAL_Delay(5);
}
void wr_dat_4spi_8bcs(uint8_t par)
{
    LCD_DCXHigh();
    LCD_CSLow();

    LCD_SCLLow();
    if (par & 0x80)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x40)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x20)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x10)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x08)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x04)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x02)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x01)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_CSHigh();
    LCD_DCXHigh();
    // HAL_Delay(1);
}

void Write(uint16_t dc, uint16_t par)
{
    if (dc == Command)
    {
        wr_cmd_4spi_8bcs(par);
    }
    else if (dc == Parameter)
    {
        wr_dat_4spi_8bcs(par);
    }
}

void HW_Reset(void)
{
    LCD_RSTHigh();
    HAL_Delay(120);
    LCD_RSTLow();
    HAL_Delay(120);
    LCD_RSTHigh();
    HAL_Delay(200);
}

void Delay(uint32_t ms)
{
    HAL_Delay(ms);
}

void initial(void)
{
    HW_Reset();

    Delay(120);

    Write(Command, 0x11); // Sleep Out

    Delay(120);

    Write(Command, 0x36);
    Write(Parameter, 0x48);

    Write(Command, 0x3A); // Interface Pixel Format
    Write(Parameter, 0x77);

    Write(Command, 0xF0); // Command Set Control
    Write(Parameter, 0xC3);

    Write(Command, 0xF0); //
    Write(Parameter, 0x96);

    Write(Command, 0xB4);
    Write(Parameter, 0x01); // 1-Dot INV

    Write(Command, 0xB7);
    Write(Parameter, 0xC6);

    Write(Command, 0xB9);
    Write(Parameter, 0x02);
    Write(Parameter, 0xE0);

    Write(Command, 0xC0);
    Write(Parameter, 0x80);
    Write(Parameter, 0x75); // VGH=15V, VGL=-10V

    Write(Command, 0xC1);
    Write(Parameter, 0x13); // 4.5V

    Write(Command, 0xC2);
    Write(Parameter, 0xA7);

    Write(Command, 0xC5);   // VCOM Control
    Write(Parameter, 0x1D); // 1.15V

    Write(Command, 0xE8);
    Write(Parameter, 0x40);
    Write(Parameter, 0x8A);
    Write(Parameter, 0x00);
    Write(Parameter, 0x00);
    Write(Parameter, 0x29);
    Write(Parameter, 0x19);
    Write(Parameter, 0xA5);
    Write(Parameter, 0x33);

    Write(Command, 0xE0);
    Write(Parameter, 0xD0);
    Write(Parameter, 0x02);
    Write(Parameter, 0x0A);
    Write(Parameter, 0x10);
    Write(Parameter, 0x11);
    Write(Parameter, 0x0C);
    Write(Parameter, 0x35);
    Write(Parameter, 0x44);
    Write(Parameter, 0x46);
    Write(Parameter, 0x3C);
    Write(Parameter, 0x15);
    Write(Parameter, 0x13);
    Write(Parameter, 0x17);
    Write(Parameter, 0x1A);

    Write(Command, 0xE1);
    Write(Parameter, 0xD0);
    Write(Parameter, 0x01);
    Write(Parameter, 0x08);
    Write(Parameter, 0x0B);
    Write(Parameter, 0x0C);
    Write(Parameter, 0x17);
    Write(Parameter, 0x35);
    Write(Parameter, 0x44);
    Write(Parameter, 0x47);
    Write(Parameter, 0x09);
    Write(Parameter, 0x18);
    Write(Parameter, 0x17);
    Write(Parameter, 0x19);
    Write(Parameter, 0x1D);

    Write(Command, 0x35);
    Write(Parameter, 0x00);

    Write(Command, 0xF0);
    Write(Parameter, 0x3C);

    Write(Command, 0xF0);
    Write(Parameter, 0x69);

    Delay(120); // Delay 120ms

    Write(Command, 0x29); // Display ON
}
